package com.infore.airsupervise.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.AppInfo;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.utils.AirLevelUtil;
import com.inqbarna.tablefixheaders.adapters.BaseTableAdapter;

/**
 * Created by xiaotao on 2017/8/16.
 */

public class HotAnalyzeTableAdapter extends BaseTableAdapter {
    private Context context;

    private String[] topTitle;
    private String[] leftTitle;
    private double[][] data;

    private int leftTitleWidth = 30;
    private int width = 30;
    private int height = 20;

    private int factorType ;  //当前数据因子类型


    public HotAnalyzeTableAdapter(Context context) {
        this.context = context;
    }

    public int getFactorType() {
        return factorType;
    }

    public void setFactorType(int factorType) {
        this.factorType = factorType;
    }

    public void updata(String[] topTitle, String[] leftTitle, double[][] data){
        this.topTitle = topTitle;
        this.leftTitle = leftTitle;
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getRowCount() {
        return leftTitle == null ? 0:leftTitle.length;
    }

    @Override
    public int getColumnCount() {
        return topTitle == null ? 0: topTitle.length;
    }

    @Override
    public View getView(int row, int column, View convertView, ViewGroup parent) {
        ViewHolder vh ;
        if(convertView == null){
            convertView =  LayoutInflater.from(context).inflate(R.layout.item_table_hot, parent, false);
            vh = new ViewHolder();
            vh.tv_content = (TextView) convertView.findViewById(android.R.id.text1);
            convertView.setTag(vh);
        }
        vh = (ViewHolder) convertView.getTag();
        switch (getItemViewType(row,column)){
            case 0 :
                convertView.setBackgroundColor(ContextCompat.getColor(context,android.R.color.white));
                break;
            case 1 :
                vh.tv_content.setText(topTitle[column]);
                convertView.setBackgroundColor(ContextCompat.getColor(context,android.R.color.white));
                break;
            case 2 :
                vh.tv_content.setText(leftTitle[row]);
                convertView.setBackgroundColor(ContextCompat.getColor(context,android.R.color.white));
                break;
            case 3 :
                vh.tv_content.setBackgroundColor(ContextCompat.getColor(context, AirLevelUtil.getFactorLevelColor(0 , factorType,data[row][column])));
                break;
        }
        return convertView;
    }

    @Override
    public int getWidth(int column) {
        if (column == -1) {
            return Math.round(leftTitleWidth * AppInfo.DENSITY);
        } else {
            return Math.round(width * AppInfo.DENSITY);
        }
    }

    @Override
    public int getHeight(int row) {
        return Math.round(height * AppInfo.DENSITY);
    }

    @Override
    public int getItemViewType(int row, int column) {
        if (row == -1 && column == -1) {
            return 0;
        } else if (row < 0) {
            return 1;
        } else if (column < 0){
            return 2;
        } else {
            return 3;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    public class ViewHolder{
        private TextView tv_content;
    }
}
